package com.zhenhe.core.web.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhenhe.core.common.controller.BaseController;
import com.zhenhe.core.common.entity.RespJson;
import com.zhenhe.core.common.entity.TUser;
import com.zhenhe.core.common.entity.UserEntity;
import com.zhenhe.core.common.global.signIn.LoginInfoUtil;
import com.zhenhe.core.common.utils.http.IPUtil;
import com.zhenhe.core.modules.service.LoginService;
import com.zhenhe.core.modules.service.TUserService;

@Controller
@RequestMapping("login")
public class LoginController extends BaseController{

	private static final Logger log = LoggerFactory.getLogger("login");
	
	@Resource
	private LoginService loginService;
	
	@Resource
	private TUserService tUserService;
	
	@RequestMapping("index")
	public String goLogin(){
		return "back/login/login";
	}
	
	@RequestMapping("passwordUpdateInit")
	public String passwordUpdateInit(HttpServletRequest request, UserEntity user){
		return "back/login/passwordUpdate";
	}
	
	@RequestMapping("signin")
	@ResponseBody
	public RespJson login(HttpServletRequest request, TUser user){
		RespJson result = null;
		try{
			result = loginService.login(request, user);
		}catch(Throwable e){
			log.error("用户{}尝试登陆，系统出错：{},ip为{}",user.getUserAccount(),e.getMessage(),IPUtil.getIp(request));
			throw e;
		}
		log.info("用户{}尝试登陆，结果：{},ip为{}",user.getUserAccount(),result.getMsg(),IPUtil.getIp(request));
		return result;
	}
	
	@RequestMapping("signout")
	public String signout(HttpServletRequest request,HttpServletResponse resp, UserEntity user){
		LoginInfoUtil.signOut(request);
		return "redirect:/";
	}
}
